package com.atguigu.write;

import com.atguigu.bean.Movie;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Bulk;
import io.searchbox.core.Index;

import java.io.IOException;

public class Es02_BulkWrite {
    public static void main(String[] args) throws IOException {
        //1.创建客户端工厂
        JestClientFactory jestClientFactory = new JestClientFactory();

        //2.设置连接属性
        jestClientFactory.setHttpClientConfig(new HttpClientConfig.Builder("http://hadoop102:9200").build());

        //3.获取连接
        JestClient jestClient = jestClientFactory.getObject();

        //4.批量写入数据
        Movie movie106 = new Movie("106", "你好李焕英");
        Movie movie107 = new Movie("107", "色戒");
        Movie movie108 = new Movie("108", "爱情公寓之紫禁城之战");
        Movie movie109 = new Movie("109", "蜘蛛侠3：英雄归来");
        Movie movie110 = new Movie("110", "月光骑士");

        Index index106 = new Index.Builder(movie106).id("1006").build();
        Index index107 = new Index.Builder(movie107).id("1007").build();
        Index index108 = new Index.Builder(movie108).id("1008").build();
        Index index109 = new Index.Builder(movie109).id("1009").build();
        Index index110 = new Index.Builder(movie110).id("1010").build();

        Bulk bulk = new Bulk.Builder()
                .addAction(index106)
                .addAction(index107)
                .addAction(index108)
                .addAction(index109)
                .addAction(index110)
                .defaultType("movie")
                .defaultIndex("movie_chn_2022")
                .build();
        jestClient.execute(bulk);

        //关闭连接
        jestClient.shutdownClient();
    }
}
